package com.edu.service.impl;

import com.edu.bean.Book;
import com.edu.mapper.BookMapper;
import com.edu.service.BookService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Resource
    private BookMapper bookMapper;

    @Override
    public List<Book> getAllBook() {
        return bookMapper.selectAllBooks();
    }

    @Override
    public int addBook(Book book) {
        int row = 0;
        if (bookMapper.selectBookByName(book.getBookName()) != null) {
            row = -1;
        }else {
            row = bookMapper.insertBook(book);
        }
        return row;
    }

    @Override
    public Book getBook(String bookName) {
        return bookMapper.selectBookByName(bookName);
    }

    @Override
    public List<Book> getBooks(Book book) {
        return bookMapper.selectBooks(book);
    }

    @Override
    public int delBookById(int bookId) {
        int row = 0;
        if (bookMapper.deleteBookByBookId(bookId) == 1){
            row = 1;
        }
        return row;
    }

    @Override
    public int updateBookById(Book book) {
        int row = 0;
        if (bookMapper.updateBookByBookId(book) == 1){
            row = 1;
        }
        return row;
    }
}
